﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 用户收发/货地址仓储
/// </summary>
public class MdAddressRep : BaseRep<MdAddressModel>
{
    private static string sqlFields = string.Format(@"
        `address_id`,
        `name`,
        `phone`,
        `email`,
        `state`,
        `city`,
        `province`,
        `region`,
        `country`,
        `zip_code`,
        `address`,
        `user_id`,
        `address_type`,
        `is_default`,
        `status`,
        `del_flag`,
        `create_by`,
        `create_time`,
        `update_by`,
        `update_time`
    ");

    /// <summary>
    /// 通过编号获取地址信息
    /// </summary>
    /// <param name="id">地址编号</param>
    /// <returns>MdAddressModel</returns>
    public MdAddressModel? GetMdAddressById(long id)
    {
        string sqlWhere = string.Format(@" and `del_flag` = '0' and `address_id` = @AddressId ");

        return QuerySingle(sqlFields, sqlWhere, new { AddressId = id });
    }

    /// <summary>
    /// 获取地址列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒叙</param>
    /// <returns>IEnumerable<MdAddressModel></returns>
    public IEnumerable<MdAddressModel> GetMdAddressList(MdAddressModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Name))
        {
            sqlWhere += " and `name` like concat('%', @Name, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Phone))
        {
            sqlWhere += " and `phone` = @Phone ";
        }
        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlWhere += " and `email` = @Email ";
        }
        if (!string.IsNullOrWhiteSpace(model.State))
        {
            sqlWhere += " and `state` like concat('%', @State, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.City))
        {
            sqlWhere += " and `city` like concat('%', @City, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Province))
        {
            sqlWhere += " and `province` like concat('%', @Province, '%') ";
        }
        if(!string.IsNullOrWhiteSpace(model.Region))
        {
            sqlWhere += " and `region` like concat('%', @Region, '%') ";
        }
        if(!string.IsNullOrWhiteSpace(model.Country))
        {
            sqlWhere += " and `country` like concat('%', @Country, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.ZipCode))
        {
            sqlWhere += " and `zip_code` = @ZipCode ";
        }
        if (model.UserId > 0)
        {
            sqlWhere += " and `user_id` = @UserId ";
        }
        if(!string.IsNullOrWhiteSpace(model.AddressType))
        {
            sqlWhere += " and `address_type` = @AddressType ";
        }
        if(!string.IsNullOrWhiteSpace(model.IsDefault))
        {
            sqlWhere += " and `is_default` = @IsDefault ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取地址列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<MdAddressModel></returns>
    public Page<MdAddressModel> GetMdAddressPageList(MdAddressModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Name))
        {
            sqlWhere += " and `name` like concat('%', @Name, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Phone))
        {
            sqlWhere += " and `phone` = @Phone ";
        }
        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlWhere += " and `email` = @Email ";
        }
        if (!string.IsNullOrWhiteSpace(model.State))
        {
            sqlWhere += " and `state` like concat('%', @State, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.City))
        {
            sqlWhere += " and `city` like concat('%', @City, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Province))
        {
            sqlWhere += " and `province` like concat('%', @Province, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Region))
        {
            sqlWhere += " and `region` like concat('%', @Region, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Country))
        {
            sqlWhere += " and `country` like concat('%', @Country, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.ZipCode))
        {
            sqlWhere += " and `zip_code` = @ZipCode ";
        }
        if (model.UserId > 0)
        {
            sqlWhere += " and `user_id` = @UserId ";
        }
        if (!string.IsNullOrWhiteSpace(model.AddressType))
        {
            sqlWhere += " and `address_type` = @AddressType ";
        }
        if (!string.IsNullOrWhiteSpace(model.IsDefault))
        {
            sqlWhere += " and `is_default` = @IsDefault ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过编号更新地址
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>long</returns>
    public long UpdateMdAddressById(MdAddressModel model)
    {
        var sqlSet = string.Empty;

        #region ===set===
        if (model.Name != null)
        {
            sqlSet += " `name` = @Name, ";
        }

        if (model.Phone != null)
        {
            sqlSet += " `phone` = @Phone, ";
        }

        if (model.Email != null)
        {
            sqlSet += " `email` = @Email, ";
        }

        if (model.State != null)
        {
            sqlSet += " `state` = @State, ";
        }

        if (model.City != null)
        {
            sqlSet += " `city` = @City, ";
        }

        if (model.Province != null)
        {
            sqlSet += " `province` = @Province, ";
        }

        if (model.Region != null)
        {
            sqlSet += " `region` = @Region, ";
        }

        if (model.Country != null)
        {
            sqlSet += " `country` = @Country, ";
        }

        if (model.ZipCode != null)
        {
            sqlSet += " `zip_code` = @ZipCode, ";
        }

        if (model.Address != null)
        {
            sqlSet += " `address` = @Address, ";
        }

        if (model.UserId > 0)
        {
            sqlSet += " `user_id` = @UserId, ";
        }

        if (model.AddressType != null)
        {
            sqlSet += " `address_type` = @AddressType, ";
        }

        if (model.IsDefault != null)
        {
            sqlSet += " `is_default` = @IsDefault, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `del_flag` = '0' and `address_id` = @AddressId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过编号删除地址
    /// </summary>
    /// <param name="ids">编号多个逗号分隔</param>
    /// <returns>int</returns>
    public int DeleteMdAddressById(string ids)
    {
        string sqlWhere = " and `address_id` in @AddressIds ";

        return Delete(sqlWhere, new { AddressIds = ids.Split(',').ToArray() });
    }
}

